<template>
	<view class="bg-white padding-lr">
		<u-form :model="form" ref="uForm" label-width="auto">
			<u-form-item label="编码" prop="code" required>
				<u-input v-model="form.code" />
			</u-form-item>
			<u-form-item label="名称" prop="name" required>
				<u-input v-model="form.name" />
			</u-form-item>
			<u-form-item label="序号" prop="seq" required>
				<u-input type="number" v-model="form.seq" />
			</u-form-item>
			<view class="padding-tb">
				<u-button type="primary" @click="save()">保存</u-button>
			</view>
		</u-form>
	</view>
</template>

<script>
	export default {
		onLoad: function(option) {
			if (option.id) {
				this.form.id = option.id;
				this.get();
			}
		},
		onShow: function() {},
		onReady: function() {
			this.$refs.uForm.setRules(this.rules);
		},
		onReachBottom: function() {},
		data() {
			return {
				form: {
					id: null,
					code: null,
					name: null,
					seq: 1
				},
				rules: {
					code: [{
						required: true,
						message: "请填写编码"
					}],
					name: [{
						required: true,
						message: "请填写名称"
					}],
					seq: [{
						required: true,
						message: "请填写序号"
					}]
				}
			};
		},
		methods: {
			save() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$http.post(`/admin/sys/rbac/perm/save`, this.form, res => {
							const eventChannel = this.getOpenerEventChannel();
							eventChannel.emit('refresh', {});
							this.$utils.modal("提示", "保存成功！", false, flag => {
								if (flag) {
									uni.navigateBack();
								}
							})
						}, false, true, true);
					}
				});
			},
			get() {
				this.$http.get(`/admin/sys/rbac/perm/get/${this.form.id}`, {}, res => {
					this.form = res.data || this.form;
				}, false, true, true);
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>
